//
//  CategorySortView.m
//  quanminzhekou
//
//  Created by zxl on 15/1/13.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CategorySortView.h"

@interface CategorySortView ()
@end

@implementation CategorySortView

- (void)setButtonsBorder {
    self.layer.masksToBounds = YES;
    self.layer.cornerRadius = 2.0;
    
    [self onTapDefulteBtn:nil];
}

#pragma mark -
- (UIImageView*) saleImg {
    if (nil == _saleImg) {
        _saleImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"category_sort_up"]];
        _saleImg.hidden = YES;
        [self.salesBtn addSubview:_saleImg];
    }
    _saleImg.frame = CGRectMake(CGRectGetWidth(self.salesBtn.frame) - 9 - 5, (26 - 12) / 2.0, 9, 12);
    
    return _saleImg;
}

- (UIImageView*) priceImg {
    if (nil == _priceImg) {
        _priceImg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"category_sort_up"]];
        _priceImg.hidden = YES;
        [self.priceBtn addSubview:_priceImg];
    }
    _priceImg.frame = CGRectMake(CGRectGetWidth(self.priceBtn.frame) - 9 - 5, (26 - 12) / 2.0, 9, 12);
    
    return _priceImg;
}

- (void)setAllButtonsDefulteStatue {
    [self setButtonDefulteStatue:self.defulteBtn];
    [self setButtonDefulteStatue:self.salesBtn];
    [self setButtonDefulteStatue:self.priceBtn];
    [self setButtonDefulteStatue:self.filterBtn];
}

- (void)setButtonDefulteStatue:(UIButton*)btn {
    btn.selected = NO;
    btn.backgroundColor = [UIColor whiteColor];
    if ([btn isEqual:self.salesBtn]) {
        self.saleImg.hidden = YES;
        [self setImg:self.saleImg isUp:YES];
    } else if ([btn isEqual:self.priceBtn]) {
        self.priceImg.hidden = YES;
        [self setImg:self.priceImg isUp:YES];
    }
}

- (void)setButtonSelectStatue:(UIButton*)btn {
    btn.selected = YES;
    btn.backgroundColor = [UIColor colorWithRed:0xf3 / 255.0 green:0xf4 / 255.0 blue:0xf4 / 255.0 alpha:1.0];
    if ([btn isEqual:self.salesBtn]) {
        self.saleImg.hidden = NO;
        [self setImg:self.saleImg isUp:YES];
    } else if ([btn isEqual:self.priceBtn]) {
        self.priceImg.hidden = NO;
        [self setImg:self.priceImg isUp:YES];
    }
}

- (void)setImg:(UIImageView*)img isUp:(BOOL)isUp{
    if (isUp) {
        img.image = [UIImage imageNamed:@"category_sort_up"];
    } else {
        img.image = [UIImage imageNamed:@"category_sort_down"];
    }
}

#pragma mark - IBAction
- (IBAction)onTapDefulteBtn:(id)sender {
    if (self.defulteBtn.selected) {
        return;
    }
    
    self.isChangedSortCondition = YES;
    
    if (self.tapDefulteBtnHandler) {
        [self setAllButtonsDefulteStatue];
        [self setButtonSelectStatue:self.defulteBtn];
        self.tapDefulteBtnHandler();
    }
}

- (IBAction)onTapSalesBtn:(id)sender {
    self.isChangedSortCondition = YES;
    
    if (self.tapSalesBtnHandler) {
        self.filterBtn.selected = NO;
        if (self.salesBtn.selected) {
            if ([self.saleImg.image isEqual:[UIImage imageNamed:@"category_sort_up"]]) {
                [self setImg:self.saleImg isUp:NO];
            } else {
                [self setImg:self.saleImg isUp:YES];
            }
        } else {
            [self setAllButtonsDefulteStatue];
            [self setButtonSelectStatue:self.salesBtn];
        }
        
        self.tapSalesBtnHandler();
    }
}

- (IBAction)onTapPriceBtn:(id)sender {
    self.isChangedSortCondition = YES;
    
    if (self.tapPriceBtnHandler) {
        self.filterBtn.selected = NO;
        if (self.priceBtn.selected) {
            if ([self.priceImg.image isEqual:[UIImage imageNamed:@"category_sort_up"]]) {
                [self setImg:self.priceImg isUp:NO];
            } else {
                [self setImg:self.priceImg isUp:YES];
            }
        } else {
            [self setAllButtonsDefulteStatue];
            [self setButtonSelectStatue:self.priceBtn];
        }
        
        self.tapPriceBtnHandler();
    }
}

- (IBAction)onTapFilterBtn:(id)sender {
    if (self.tapFilterBtnHandler) {
        self.filterBtn.selected = !self.filterBtn.selected;
        
        self.tapFilterBtnHandler();
    }
}
@end
